//
//  ViewController.m
//  UI10_homework
//
//  Created by aaaaaa on 16/6/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, SecondViewControllerDelegate>

@property (nonatomic, retain) NSMutableDictionary *dictionary;
@property (nonatomic, retain) NSArray *keysArray;
@property (nonatomic, retain) UITableView *tableView;


@end

@implementation ViewController

- (void)dealloc
{
    [_dictionary release];
    [_keysArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self config];
    
    [self handleData];
    
    
    [self createTableView];
}



- (void)config{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"联系人";
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.dictionary = [[NSMutableDictionary alloc] init];
    self.keysArray = [[NSArray alloc] init];
}

/** 解析数据*/
- (void)handleData{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Contant" ofType:@"plist"];
    self.dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    
    self.keysArray = [[self.dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
}


- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64) style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

}


//必须实现协议
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    NSArray *array = [self.dictionary objectForKey:[self.keysArray objectAtIndex:indexPath.section]];
    NSDictionary *dic = array[indexPath.row];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 414, 100)];
    
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[dic objectForKey:@"header"]]];
    imageView.frame = CGRectMake(15 + 30, (100 - 90) / 2, 90, 90);
    imageView.layer.masksToBounds = YES;
    //圆角设置为控件宽度的一半 图像为圆形
    imageView.layer.cornerRadius = imageView.bounds.size.width / 2;
    [view addSubview:imageView];
    [imageView release];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(90 + 15 + 15 + 30, 0, 414 - 90 - 15, 100)];
    lable.text = [dic objectForKey:@"name"];
    [view addSubview:lable];
    [lable release];
    
    cell.accessoryView = view;
    
    return cell;
}

//必须实现协议 分区里的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSArray *array =[self.dictionary objectForKey:[self.keysArray objectAtIndex:section]];
    return array.count;
}

//分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.keysArray.count;
}

//分区头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.keysArray[section];
}

//侧边栏
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.keysArray;
}

//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}


//点击tableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SecondViewController *secondVc = [[SecondViewController alloc] init];
    secondVc.delegate = self;
    NSArray *array = [self.dictionary objectForKey:self.keysArray[indexPath.section]];
    
    secondVc.dictionary = array[indexPath.row];
    secondVc.indexPath = indexPath;
    [self.navigationController pushViewController:secondVc animated:YES];
    [secondVc release];
}

//实现协议方法传值
- (void)preservation:(NSDictionary *)dic withIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *array = [self.dictionary objectForKey:self.keysArray[indexPath.section]];
    array[indexPath.row] = dic;
//    [self.dictionary setValue:array forKey:self.keysArray[indexPath.section]];
    //更新数据
    [self.tableView reloadData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
